package viz.controller.core
{
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import viz.model.flare.decoders.JSON2StateMap;
	import viz.model.flare.decoders.XML2StateMap;
	import viz.model.flare.proxies.StateDataProxy;

	public class DecodeIncomingDataCmd extends SimpleCommand
	{
		override public function execute(note:INotification):void{
			if(note.getBody() == null)return;
			var data:Object = decodeData( note.getType(),note.getBody() );
			StateDataProxy( facade.retrieveProxy(StateDataProxy.NAME)).setData( data );	
		}
		
		private function decodeData( type:String, data:Object ):Object{
			switch(type){
				case "xml":
					return decodeXML( new XML( data ) )
				case "fsm":
					return decodeJSON( String( data ) )
				default:
					throw new Error("unknown data type");
					
			}
		}
		
		private function decodeXML( xml:XML ):Object{
			return new XML2StateMap( xml ).decode();
		}
		
		private function decodeJSON( s:String ):Object{
			return new JSON2StateMap( s ).decode();
		}
		
	}
}